package com.example.hibernate;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.example.AddressBook;
import com.example.domain.Person;

public class HibernateAddressBook implements AddressBook {
	private SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
	
	public void addPerson(Person person) {
		Session s = sessionFactory.getCurrentSession();
		s.beginTransaction();
		s.save(person);
		s.getTransaction().commit();
	}
	
	@SuppressWarnings("unchecked")
	public List<Person> getPersons() {
		Session s = sessionFactory.getCurrentSession();
		
		s.beginTransaction();
		List<Person> persons = (List<Person>) s.createQuery("from Person").list();
		s.getTransaction().commit();
		
		return persons;
	}
}
